
/**
 * @author lenovo
 *
 */

package view;

import control.PassengerController;
import model.Passenger;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MealSeat implements ActionListener{
    /*LogIn and background variable*/
    public static JFrame jfra;
    public static JPanel jpanMS,jpan1, jpan2,myPanel;
    public static JLabel company, MyTripLabel, LogInLabel, ServiceLabel, VIPLabel, ModifyLabel, ContactLabel,label;
    public static ImageIcon background;
    /*Seat Choice Variable*/
    public static JPanel jpanSeat, jpansChoice, jpansButton,jpanScroll;;
    public static JButton seat[][], sclear, ssave, next;
    public static JLabel passage1,passage2, row;
    public static int Row, Column;
    public static JScrollPane seatButton;
    /*Meal Choice Variable*/
    public static JPanel jpanMeal,jpanMealCS, jpan2Meal;
    public static JButton clearM,saveM;
    public static ButtonGroup buttonGroup1;
    public static JRadioButton meal1,meal2,meal3,meal4;
    /*Create an object to write seat selection information and meal selection information
      into the database according to the booking number*/
    public static PassengerController passengerController = new PassengerController();
    public static LogIn logIn = new LogIn();
    public static boolean choose = false;
    public static int state[][];


    public MealSeat() {
        jfra = new JFrame();

        background = new ImageIcon("src/view/image/background.png");//Create the background picture
        label = new JLabel(background);//Add the picture to the label
        label.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());//Set the label with same height and width with the picture
        myPanel = (JPanel) jfra.getContentPane();
        myPanel.setOpaque(false);
        myPanel.setLayout(new FlowLayout());
        jfra.getLayeredPane().setLayout(null);

        //Create the menu bar
        jpan1 = new JPanel();
        company = new JLabel("          Check-In Kiosk                    ", JLabel.LEFT);
        Font fnt = new Font("Serief", Font.BOLD, 40);// Setting the size and style of the font
        company.setFont(fnt);
        MyTripLabel = new JLabel("MyTrip          ", JLabel.RIGHT);
        LogInLabel = new JLabel("LogIn          ", JLabel.RIGHT);
        LogInLabel.setBackground(Color.YELLOW);
        ServiceLabel = new JLabel("Service         ", JLabel.RIGHT);
        VIPLabel = new JLabel("VIP         ", JLabel.RIGHT);
        ModifyLabel = new JLabel("Modify         ", JLabel.RIGHT);
        ContactLabel = new JLabel("Contact           ", JLabel.RIGHT);
        company.setForeground(Color.blue);
        jpan1.add(company);
        jpan1.add(MyTripLabel);
        jpan1.add(LogInLabel);
        jpan1.add(ServiceLabel);
        jpan1.add(VIPLabel);
        jpan1.add(ModifyLabel);
        jpan1.add(ContactLabel);
        jpan1.setVisible(true);

        //Set the total panel2 of Seat and Meal as flow layout
        jpanMS = new JPanel(new FlowLayout());
        jpanMS.setPreferredSize(new Dimension(1150,550));
        jpan2 = new JPanel(new GridLayout(1,2,20,20));
        jpan2.setPreferredSize(new Dimension(1150, 510));
        jpan2.setVisible(true);

        //Set the format of the Seat Panel
        jpanSeat = new JPanel(new FlowLayout());// jpanSeat contains choice window and executable button
        jpanSeat.setPreferredSize(new Dimension(600, 510));//Set the size of total jpanSeat
        Border titleBorder1 = BorderFactory.createEtchedBorder();//Set border of the jpanSeat
        jpanSeat.setBorder(new TitledBorder(titleBorder1,""));
        jpanSeat.setVisible(true);

        //Set the format of the Choice Window of Seat and Add the Seat Button
        jpansChoice = new JPanel(new GridLayout(20,13,1,15));
        //jpansChoice.setPreferredSize(new Dimension(550,450));
        jpansChoice.setBorder(BorderFactory.createTitledBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black),
                "seat")));//Add the specific border of the panel
        Passenger a = passengerController.queryByBookNumber(LogIn.bookNumber);
        a.setCurrentPlaneNum(LogIn.currentPlaneNum);//设置航班号？
        state=passengerController.siteCondition(a);
        seat = new JButton[20][10]; //Set the size of the button array
        for(int i=0; i<20; i++) {
            row = new JLabel(""+(i+9));//i+4 means the row that the ordinary users can choose
            jpansChoice.add(row);//Add the row number in each row
            //create the seat button
            for(int j=0; j<3; j++) {
                seat[i][j] = new JButton();
                if(state[i+9][j+1]==1){
                    seat[i][j].setBackground(Color.GRAY);
                }
                else{
                    seat[i][j].setBackground(Color.WHITE);
                }
                seat[i][j].addActionListener(this);
                seat[i][j].setPreferredSize(new Dimension(40, 40));
                ImageIcon square=new ImageIcon("src/view/image/seat.png");//Generate the icon picture for seat
                square.setImage(square.getImage().getScaledInstance(35,35, 0));//Set the size of picture
                seat[i][j].setIcon(square);
                jpansChoice.add(seat[i][j]);
            }
            passage1 = new JLabel("| |");//Add the passage as label
            Font fontp1 = new Font("Time Roman", Font.PLAIN, 40);
            passage1.setFont(fontp1);//Set the format of the passage
            jpansChoice.add(passage1);
            for(int j=3; j<7; j++) {
                seat[i][j] = new JButton();
                if(state[i+9][j+1]==1){
                    seat[i][j].setBackground(Color.GRAY);
                }
                else{
                    seat[i][j].setBackground(Color.WHITE);
                }
                seat[i][j].addActionListener(this);
                seat[i][j].setPreferredSize(new Dimension(40, 40));
                ImageIcon square=new ImageIcon("src/view/image/seat.png");
                square.setImage(square.getImage().getScaledInstance(35,35, 0));
                seat[i][j].setIcon(square);
                jpansChoice.add(seat[i][j]);
            }
            passage2 = new JLabel("| |");//Add the passage as label
            Font fontp2 = new Font("Time Roman", Font.PLAIN, 40);
            passage2.setFont(fontp2);//Set the format of the passage
            jpansChoice.add(passage2);
            for(int j=7; j<10; j++) {
                seat[i][j] = new JButton();
                if(state[i+9][j+1]==1){
                    seat[i][j].setBackground(Color.GRAY);
                }
                else{
                    seat[i][j].setBackground(Color.WHITE);
                }
                seat[i][j].addActionListener(this);
                seat[i][j].setPreferredSize(new Dimension(40, 40));
                ImageIcon square=new ImageIcon("src/view/image/seat.png");
                square.setImage(square.getImage().getScaledInstance(35,35, 0));
                seat[i][j].setIcon(square);
                jpansChoice.add(seat[i][j]);
            }
        }

        //Add the scroll pane
        seatButton = new JScrollPane(jpansChoice);
        seatButton.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        //seatButton.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        seatButton.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        seatButton.setBounds(1, 1, 560, 440);
        jpanScroll = new JPanel(null);
        jpanScroll.setPreferredSize(new Dimension(560, 450));

        //Add executable buttons for the Seat Panel
        jpansButton = new JPanel(new FlowLayout());
        sclear = new JButton("Clear");//This button is used to clear the choice of seat.
        ssave = new JButton("Save the Seat");//This button is used to save the choice of seat.
        sclear.addActionListener(this);// Add the action listener
        ssave.addActionListener(this);// Add the action listener
        jpansButton.add(sclear);
        jpansButton.add(ssave);
        sclear.setBackground(Color.WHITE);//Set the background color of the button
        ssave.setBackground(Color.WHITE);

        //Set the format of Meal Panel
        jpan2Meal = new JPanel();
        jpan2Meal.setSize(600,510);
        Border titleBorder2 = BorderFactory.createEtchedBorder();
        jpan2Meal.setBorder(new TitledBorder(titleBorder2,""));//Set the border of the total Meal Panel

        //Set the format of the Choice Window of Meal
        jpanMeal = new JPanel(new GridLayout(4, 2));
        jpanMeal.setPreferredSize(new Dimension(450,450));
        jpanMeal.setBorder(BorderFactory.createTitledBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black),
                "meal")));//Set the specific border of the Choice Window of Meal

        //Set the format of the words of meal
        meal1 = new JRadioButton("Pizza");
        Font fontmeal = new Font("Time Roman", Font.PLAIN, 20);
        meal1.setFont(fontmeal);
        //meal1.addItemListener(this::itemStateChanged);
        meal2 = new JRadioButton("Pasta");
        meal2.setFont(fontmeal);
        //meal2.addItemListener(this::itemStateChanged);
        meal3 = new JRadioButton("Beef");
        meal3.setFont(fontmeal);
        //meal3.addItemListener(this::itemStateChanged);
        meal4 = new JRadioButton("Rice");
        meal4.setFont(fontmeal);
        //meal4.addItemListener(this::itemStateChanged);

        //Add buttons of Choice of Meal, total 4 choices for ordinary users
        buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(meal1);
        buttonGroup1.add(meal2);
        buttonGroup1.add(meal3);
        buttonGroup1.add(meal4);

        //Add picture for the Choice of Meal
        ImageIcon icon1 =new ImageIcon("src/view/image/pizza.jpg");//Generate the icon picture for pizza
        icon1.setImage(icon1.getImage().getScaledInstance(100,100,60));//Set the size of the picture
        JLabel pizza1 = new JLabel("", JLabel.CENTER);
        pizza1.setIcon(icon1);
        ImageIcon icon2 =new ImageIcon("src/view/image/pasta.jpg");//Generate the icon picture for pasta
        icon2.setImage(icon2.getImage().getScaledInstance(100,100,60));//Set the size of the picture
        JLabel pasta1 = new JLabel("", JLabel.CENTER);
        pasta1.setIcon(icon2);
        ImageIcon icon3 =new ImageIcon("src/view/image/beefstake.png");//Generate the icon picture for beefstake
        icon3.setImage(icon3.getImage().getScaledInstance(100,100,60));//Set the size of the picture
        JLabel beef1 = new JLabel("", JLabel.CENTER);
        beef1.setIcon(icon3);
        ImageIcon icon4 =new ImageIcon("src/view/image/rice.png");//Generate the icon picture for rice
        icon4.setImage(icon4.getImage().getScaledInstance(100,100,60));//Set the size of the picture
        JLabel rice1 = new JLabel("", JLabel.CENTER);
        rice1.setIcon(icon4);

        //Add the food element to the Choice of Meal panel
        jpanMeal.add(pizza1);
        jpanMeal.add(meal1);
        jpanMeal.add(pasta1);
        jpanMeal.add(meal2);
        jpanMeal.add(beef1);
        jpanMeal.add(meal3);
        jpanMeal.add(rice1);
        jpanMeal.add(meal4);

        //Add the executable buttons to the subpanel of Meal Panel
        jpanMealCS = new JPanel();
        saveM = new JButton("Save the Meal");
        saveM.setBackground(Color.WHITE);//Set the background color of the button
        saveM.addActionListener(this::actionPerformed1);//Add the action listener
        jpanMealCS.add(saveM);

        //Add the next button for the total panel of Seat and Meal
        next = new JButton("Next");
        next.addActionListener(this);//Add the action listener

        //Set the background color of panels
        meal1.setBackground(Color.WHITE);
        meal2.setBackground(Color.WHITE);
        meal3.setBackground(Color.WHITE);
        meal4.setBackground(Color.WHITE);
        jpanMS.setBackground(Color.WHITE);
        next.setBackground(Color.WHITE);
        jpanMeal.setBackground(Color.WHITE);
        jpan2Meal.setBackground(Color.WHITE);
        jpanMealCS.setBackground(Color.WHITE);
        jpan2.setBackground(Color.WHITE);
        jpanSeat.setBackground(Color.WHITE);
        jpansChoice.setBackground(Color.WHITE);
        jpansButton.setBackground(Color.WHITE);
        jpanScroll.setBackground(Color.WHITE);
        seatButton.setBackground(Color.WHITE);

        //Add panel elements to the total panel of Seat and Meal, and add jpanMS to myPanel
        myPanel.add(jpan1);
        myPanel.add(jpanMS);
        jpanMS.add(jpan2);
        jpanMS.add(next);
        jpan2.add(jpanSeat);
        jpan2.add(jpan2Meal);
        jpanSeat.add(jpanScroll);
        jpanScroll.add(seatButton);
        jpanSeat.add(jpansButton);
        jpan2Meal.add(jpanMeal);
        jpan2Meal.add(jpanMealCS);


        jfra.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));//Add layered labels to the bottom panel

        //Set panels' properties
        jfra.setBounds(300, 300, background.getIconWidth(), background.getIconHeight());
        jfra.setVisible(true);
        jpanMeal.setVisible(true);
        jpanMealCS.setVisible(true);
        seatButton.setVisible(true);
        jfra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    //Monitor each seat selection. After selection, the border turns red and the array position is recorded into the file
    //You can choose only one seat for one booking number, else there will be a warning
    public void actionPerformed(ActionEvent e) {
        JButton e1 = (JButton) (e.getSource());//Listen to the button
        String s1 = e1.getText();//Get the string of content of button
        // Control the seat, after clicking the boundary will turn red
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 10; j++) {
                if (e.getSource().equals(seat[i][j])) {
                    if (state[i + 9][j + 1] == 1) {
                        seat[i][j].setBackground(Color.GRAY);
                        JOptionPane.showMessageDialog(null, "THIS SEAT WAS CHOSEN BY OTHERS!", "Warning", JOptionPane.ERROR_MESSAGE);
                    }
                    else {
                        if ((Row == 0) || (Column == 0)) {//Judge whether the booking number has chosen one seat already
                            seat[i][j].setBackground(Color.RED);//Make the boundary turn red
                            Row = i + 9;//Record the row number
                            Column = j + 1;//Record the column number
                            //System.out.println(a + "," + b);
                        } else if ((Row == i + 9) && (Column == j + 1)) {
                            JOptionPane.showMessageDialog(null, "YOU HAVE CHOOSE THAT SEAT!", "Warning", JOptionPane.ERROR_MESSAGE);
                        } else {
                            seat[i][j].setBackground(Color.WHITE);//Make the boundary turn white
                            //System.out.println("YOU CAN ONLY CHOOSE ONE");
                            JOptionPane.showMessageDialog(null, "YOU CAN CHOOSE ONLY ONE SEAT!", "Warning", JOptionPane.ERROR_MESSAGE);

                        }
                    }
                }
                if(s1.equals("Clear")) {
                    if(state[i+9][j+1]==0) {
                        seat[i][j].setBackground(Color.WHITE);//Make all the button border turn white
                    }
                    Row=0;//Set the value be zero
                    Column=0;//Set the value be zero
                    //System.out.println(Row+","+Column);
                }
            }
        }
        if(s1.equals("Save the Seat")){
            if(Row!=0){
                String row = Row+"";
                String col = Column+"";
                JOptionPane.showMessageDialog(null, "YOUR CHOICE HAS BEEN SAVED!", "Success",JOptionPane.INFORMATION_MESSAGE);
                passengerController.selectSeat(logIn.bookNumber,LogIn.currentPlaneNum,row,col);//Add the row and the column to the file
            }
        }
        if(s1.equals("Next")){
            if((choose==true)&&(Row!=0)){
                jfra.setVisible(false);
                view.Board tips = new view.Board();//Get the board information window
            }
            else if(choose==false)
            {
                JOptionPane.showMessageDialog(null, "PLEASE CHOOSE OR SAVE YOUR MEAL!", "Warning", JOptionPane.ERROR_MESSAGE);
            }
            else if(Row==0){
                JOptionPane.showMessageDialog(null, "PLEASE CHOOSE OR SAVE YOUR SEAT!", "Warning", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    //Monitor each meal selection, and record the string to the file
    public void actionPerformed1(ActionEvent e) {
        String st = new String("");
        JButton e1 = (JButton) (e.getSource());//Listen to the button
        String s1 = e1.getText();//Get the string of content of button
        if(meal1.isSelected()){
            st= "pizza";
            choose = true;
        }
        else if(meal2.isSelected()){
            st = "pasta";
            choose = true;
        }
        else if(meal3.isSelected()){
            st = "beef";
            choose = true;
        }
        else if(meal4.isSelected()){
            st = "rice";
            choose = true;
        }
        if(s1.equals("Save the Meal")){
            JOptionPane.showMessageDialog(null, "YOUR CHOICE HAS BEEN SAVED!", "Success",JOptionPane.INFORMATION_MESSAGE);
        }
        System.out.println(passengerController.selectFood(LogIn.bookNumber,LogIn.currentPlaneNum,st));//Add the information of meal to the file
    }
}

